#include <bits/stdc++.h>
using namespace std;

int n, m, a1[100010], a[100010], c[100010], T;
bitset<100010> data[18];

int main(){
    ios::sync_with_stdio(false);
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for (int i=0; i<n; ++i) cin >> a1[i];
    for (int i=0; i<m; ++i) cin >> c[i];
    for (int i=1; i<(n<<2); i<<=2){
        cin >> data[i];
    }
    cin >> T;
    for (int i=0; i<T; ++i){
        int x0, x1, x2, x3;
        cin >> x0 >> x1 >> x2 >> x3;
        for (int i=0; i<n; ++i){
            if (i%4==0) a[i] = a1[i] ^ x0;
            else if (i%4==1) a[i] = a1[i] ^ x1;
            else if (i%4==2) a[i] = a1[i] ^ x2;
            else a[i] = a1[i] ^ x3;
        }
        long long ans = 0;
        for (int j=1; j<=m; ++j){
            ans ^= (j ^ c[j-1]);
        }
        cout << ans << endl;
    }
    return 0;
}
